package com.shoplink.launcher;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.shoplink.view.FocusLayout;
import com.shoplink.view.ScaleView;

public class MainActivity extends Activity implements OnClickListener,
		OnFocusChangeListener {

	private ScaleView myWifi, shoplink, myApp, picture, settings;
	private FocusLayout focusLayout;
	private TextView dateText, weekText, timeText;
	private ImageView wifi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startService(new Intent("com.shoplink.systemtime.contr.ACTION"));
		myWifi = (ScaleView) findViewById(R.id.my_wifi);
		shoplink = (ScaleView) findViewById(R.id.shoplink);
		myApp = (ScaleView) findViewById(R.id.my_app);
		picture = (ScaleView) findViewById(R.id.my_pictrue);
		settings = (ScaleView) findViewById(R.id.my_settings);
		focusLayout = (FocusLayout) findViewById(R.id.menu_layout);
		dateText = (TextView) findViewById(R.id.date);
		weekText = (TextView) findViewById(R.id.week);
		timeText = (TextView) findViewById(R.id.time);
		wifi = (ImageView) findViewById(R.id.wifi_icon);

		myWifi.setOnClickListener(this);
		myWifi.setOnfocusListen(this);
		shoplink.setOnClickListener(this);
		shoplink.setOnfocusListen(this);
		myApp.setOnClickListener(this);
		myApp.setOnfocusListen(this);
		picture.setOnClickListener(this);
		picture.setOnfocusListen(this);
		settings.setOnClickListener(this);
		settings.setOnfocusListen(this);
		focusLayout.addFocusView(true);

		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_TIME_TICK);
		filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
		registerReceiver(broadcastReceiver, filter);
		handler.post(getWeekTimeRunnable);

		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Intent intent = new Intent();
					intent.setClassName("com.shoplink.tv",
							"com.shoplink.tv.MainActivity");
					startActivity(intent);
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		}, 3000);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return false;
		} else {
			NetworkInfo[] info = cm.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
		};
	};
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		public void onReceive(android.content.Context arg0, Intent arg1) {
			String action = arg1.getAction();

			if (action.equals("android.net.conn.CONNECTIVITY_CHANGE")) {

				handler.post(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						changeWifi();
					}
				});
				return;
			}
			if (action.equals(Intent.ACTION_TIME_TICK)) {
				handler.post(getWeekTimeRunnable);
				return;
			}

		};
	};

	private void changeWifi() {
		if (isNetworkAvailable(getApplicationContext())) {
			wifi.setBackgroundResource(R.drawable.wifi_title_icon);
		} else {
			wifi.setBackgroundResource(R.drawable.wifi_title_dis);
		}
	}

	Runnable getWeekTimeRunnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			String time = getCurTime();
			String mWay = getcurrentDayOfWeekValue();
			String date = getDate();
			if ("1".equals(mWay)) {
				mWay = getResources().getString(R.string.week_7);
			} else if ("2".equals(mWay)) {
				mWay = getResources().getString(R.string.week_1);
			} else if ("3".equals(mWay)) {
				mWay = getResources().getString(R.string.week_2);
			} else if ("4".equals(mWay)) {
				mWay = getResources().getString(R.string.week_3);
			} else if ("5".equals(mWay)) {
				mWay = getResources().getString(R.string.week_4);
			} else if ("6".equals(mWay)) {
				mWay = getResources().getString(R.string.week_5);
			} else if ("7".equals(mWay)) {
				mWay = getResources().getString(R.string.week_6);
			}
			String[] strs = date.split("\\.");
			dateText.setText(strs[0] + "月" + strs[1] + "日");
			weekText.setText(mWay);
			timeText.setText(time);
		}
	};

	public static String getcurrentDayOfWeekValue() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		return mWay;
	}

	public static String getCurTime() {
		long currentTime = System.currentTimeMillis();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
		Date date = new Date(currentTime);
		return formatter.format(date);
	}

	public static String getDate() {
		long currentTime = System.currentTimeMillis();
		SimpleDateFormat formatter = new SimpleDateFormat("MM.dd");
		Date date = new Date(currentTime);
		return formatter.format(date);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub

		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(broadcastReceiver);
		super.onDestroy();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.my_wifi) {
			startActivity(new Intent(this, NetworkSettingsActivity.class));
		} else if (arg0.getId() == R.id.my_pictrue) {
			startActivity(new Intent(this, ScreenZoomActivity.class));
		} else if (arg0.getId() == R.id.shoplink) {

			try {
				Intent intent = new Intent();
				intent.setClassName("com.shoplink.tv",
						"com.shoplink.tv.MainActivity");
				startActivity(intent);
			} catch (Exception e) {
				// TODO: handle exception
			}

		} else if (arg0.getId() == R.id.my_settings) {
			Intent intent = new Intent();
			intent.setClassName("com.android.tv.settings",
					"com.android.tv.settings.MainSettings");
			startActivity(intent);

		} else if (arg0.getId() == R.id.my_app) {
			startActivity(new Intent(this, MyAppActivity.class));
		}
	}

	@Override
	public void onFocusChange(View arg0, boolean arg1) {
		// TODO Auto-generated method stub
		focusLayout.onFocusChange(arg0, arg1);
	}

}
